package com.yunzheng.modoules.enrollAndLogin.login.servlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yunzheng.modoules.enrollAndLogin.login.entity.Admin;
import com.yunzheng.modoules.enrollAndLogin.login.service.UserServiceImpl;
import com.yunzheng.modoules.response.ResponseWrapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;

@WebServlet(name = "LoginServlet",value = "/LoginServlet")
public class LoginServlet extends HttpServlet {
    Admin admin = new Admin();
    UserServiceImpl userService = new UserServiceImpl();


    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        // response.setContentType("text/html;charset=UTF-8");
        // request.setCharacterEncoding("UTF-8");

//        String phone_num = request.getParameter("log_account");
//        String password = request.getParameter("log_pwd");

        //获取请求体数据
        BufferedReader reader = request.getReader();
        String readLine = reader.readLine();
        //将JSON字符转化为JAVA对象
        JSONObject jsonObject = JSON.parseObject(readLine);
        String phone_num = jsonObject.getString("phone_num");
        String password = jsonObject.getString("password");

        HttpSession session = request.getSession();

        try {
            if (phone_num.equals(admin.getAdminPhone())  && password.equals(admin.getAdminPwd())){
                ResponseWrapper responseWrapper = null;
                responseWrapper = ResponseWrapper.ADMIN_SUCCESS();
                // request.setAttribute("requestWrapper",responseWrapper);
                String s = JSON.toJSONString(responseWrapper);
                PrintWriter writer = response.getWriter();
                writer.println(s);
                System.out.println(s);
                //response.sendRedirect("AfterLogin_Manage.jsp");
            }
            else if (userService.isLogin(phone_num,password)){

                    //request.getSession().setAttribute("phone_num",phone_num);
                    ResponseWrapper responseWrapper = null;
                    session.setAttribute("phone_num", phone_num);
                    responseWrapper = ResponseWrapper.USER_SUCCESS();
                    // request.setAttribute("requestWrapper",responseWrapper);
                    String s = JSON.toJSONString(responseWrapper);
                    PrintWriter writer = response.getWriter();
                    writer.println(s);
                    System.out.println(s);
                    //response.sendRedirect("AfterLogin_User.jsp");

            }
             else if(!userService.isExist(phone_num) && !phone_num.equals(admin.getAdminPhone())){
                ResponseWrapper responseWrapper = null;
                responseWrapper = ResponseWrapper.ACCOUNT_FAILURE();
                // request.setAttribute("requestWrapper",responseWrapper);
                String s = JSON.toJSONString(responseWrapper);
                PrintWriter writer = response.getWriter();
                writer.println(s);
                System.out.println(s);
                //request.getRequestDispatcher("Login.jsp").forward(request,response);
            }
            else if (!userService.isLogin(phone_num,password) && !password.equals(admin.getAdminPwd())) {
                ResponseWrapper responseWrapper = null;
                responseWrapper = ResponseWrapper.PWD_FAILURE();
                // request.setAttribute("requestWrapper",responseWrapper);
                String s = JSON.toJSONString(responseWrapper);
                PrintWriter writer = response.getWriter();
                writer.println(s);
                System.out.println(s);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }


    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}

